from rpy2.robjects.packages import importr
from rpy2.robjects import default_converter, pandas2ri
from rpy2.robjects.conversion import localconverter

# 导入psych包
psych = importr('psych')
pandas2ri.activate()


def cronbach_alpha(df):
    """
        describe: 信度分析
        params:     df -> python的dataframe
        return:     kmo -> KMO值
                    bt_kf -> Bartlett球形度检验的卡方值
                    free -> 自由度
                    p -> Bartlett球形度检验的P值
    """

    # # 将数据转换为R语言中的数据框
    # r_data = pandas2ri.py2rpy(df)
    # # 计算信度和项-总相关
    # alpha = psych.alpha(r_data)
    with localconverter(default_converter + pandas2ri.converter):
        r_data = pandas2ri.py2rpy(df)
        alpha = psych.alpha(r_data)
    return alpha
